<?php
namespace application\system;
/**
 * Handles all queries to the database
 *
 * @package Concerto
 * @subpackage System
 * @author Jason Raede <jason@torchdm.com>
 */
class DB {
	
	/**
	 * Connects to the database using constants defined in cn-config.php
	 */
	public static function connect() {
		mysql_connect(CN_DB_HOST, CN_DB_USER, CN_DB_PASSWORD);
		mysql_select_db(CN_DB_NAME);
	}
	
	/**
	 * Checks if a column exists in a table
	 *
	 * @param string $table
	 * @param string $column
	 * @return bool
	 */
	public static function columnExists($table, $column) {
		$results = self::getResults("SHOW COLUMNS FROM `$table`");
		foreach($results as $result) {
			if($result->Field == $column) return TRUE;
		}
		return FALSE;
		
	}
	
	/**
	 * Returns an array of variables, when querying a single column on a number of rows
	 *
	 * Example: Only getting the `date` column from all rows in an `events` table. This would
	 * return an array of dates
	 *
	 * @param string $query The MySQL query to execute
	 * @return array
	 */
	public static function getVarArray($query) {
		$results = mysql_query($query);
		$return = array();
		if($results) {
			while($row = mysql_fetch_row($results)) {
				$return[] = $row[0];
			}
		}
		return $return;
	
	}
	
	/**
	 * Returns an array of results, either as an object or an array
	 *
	 * @param string $query 		The MySQL query to execute
	 * @param string $type			The type of data to return. Either 'object' or 'array'
	 * @param string $class			If $type is 'object', which class to instantiate? (default is stdClass)
	 * @param int $contentType		The content type of the object being instantiated, which is passed to the constructor. Only used if $class is 'content'
	 * @return array An array of either objects or arrays 
	 */
	public static function getResults($query, $type = 'object', $class = null, $constructorData = null) {
		//echo $query;
		
		$return = array();
		
		$results = mysql_query($query) or die(mysql_error()); //db_query::db_error(mysql_error());
		
		switch($type) {
			case 'object':
				if($class) {
					if($constructorData) {
						while($row = mysql_fetch_object($results, $class, array($constructorData))) {
							$return[] = $row;
						}
					}
					else {
						while($row = mysql_fetch_object($results, $class)) {
							$return[] = $row;
						}
					}
					
				}
				else {
					while($row = mysql_fetch_object($results)) {
						$return[] = $row;
					}
				}
				break;
			case 'array':
				while($row = mysql_fetch_assoc($results)) {
					$return[] = $row;
				}
				break;
		}
		return $return;
	}
	
	/**
	 * Returns the number of rows that match the query
	 *
	 * @param string $query The MySQL query to execute
	 * @return int
	 */
	public static function getCount($query) {
		$results = mysql_query($query) or die(mysql_error());
		return mysql_num_rows($results);
	}
	
	/**
	 * Gets the value of a single column in a single row
	 *
	 * @param string $query The MySQL query to execute
	 * @return mixed
	 */
	public static function getVar($query) {
		$results = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_row($results);
		return $row[0];
	}
	
	/**
	 * Returns the first matching row of the query
	 *
	 * @param string $query		The MySQL query to execute
	 * @param string $return	The type of data to return ('object' or 'array')
	 * @param string $class		The class to instantiate, if $return is 'object'
	 * @return array|object
	 */
	public static function getRow($query, $return = 'object', $class = '', $contentType = null) {
		//echo $query;
		$results = mysql_query($query) or die(mysql_error());
		switch($return) {
			case 'object':
				if($class) {
					return mysql_fetch_object($results, $class, array($contentType));
				}
				else {
					return mysql_fetch_object($results);
				}
				break;
			case 'array':
				return mysql_fetch_assoc($results);
				break;
		}
		return FALSE;
	}
	
	/**
	 * Execute a query
	 * 
	 * @param string $query The MySQL query to execute
	 * @return bool
	 */
	public static function query($query) {
		//echo "<h1>Query</h1>".$query;
		$query = mysql_query($query) or die(mysql_error());
		if(!$query) {
			return FALSE;
		}
		return TRUE;
	}
	
	
	/**
	 * Updates the values in an existing row(s)
	 *
	 * @param string $table		The table to update
	 * @param string $key		The column name to match in the WHERE clause (usually the primary key of the table)
	 * @param mixed $val		The value to match to $key in the WHERE clause (WHERE `$key` = '$val')
	 * @param array $data		An array of column=>value
	 * @return bool
	 */
	public static function update($table, $key, $val, $data) {
		if($table && $key && $val && $data) {
			$updates = array();
			foreach($data as $column=>$value) {
				$updates[] = "`$column`='$value'";
			}
			$updates = implode(', ', $updates);
			if(self::query("UPDATE `$table` SET $updates WHERE `$key`='$val' LIMIT 1")) return TRUE;
			else return FALSE;
		}
		return FALSE;
	}
	
	/**
	 * Returns the primary key of the most recently inserted row
	 *
	 * @return int
	 */
	public static function lastInsert() {
		return mysql_insert_id();
	}
	
	/**
	 * Inserts data to a table
	 *
	 * @param string $table		The name of the table
	 * @param array $data		An array of column=>value pairs
	 * @return bool
	 */
	public static function insert($table, $data) {
		if($table && $data) {
			$columns = array();
			$values = array();
			foreach($data as $column=>$value) {
				$columns[] = $column;
				$values[] = $value;
			}
			
			$columns = '`'.implode('`, `', $columns).'`';
			$values = "'".implode("', '", $values)."'";
			if(self::query("INSERT INTO `$table` ($columns) VALUES($values)")) {
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		return FALSE;
	}
}
?>